﻿using System.Windows;
using System.Windows.Controls;
using WPFTestSolution.Bean;

namespace WPFTestSolution.Pages.Controls.ItemsControls {
    
    /**
     * https://www.bilibili.com/video/BV1bV411A7Eq?p=53
     * 053-ListBox列表控件
     * https://www.wpfsoft.com/2023/09/01/1770.html
     */
    public partial class _53_ListBox_Page : Page {
        public _53_ListBox_Page() {
            InitializeComponent();
            this.Title = "053-ListBox列表控件";
            
            //Items属性是一个只读属性，所以我们只能能过Items的Add方法向集合增加元素。
            this.listbox0.Items.Add(new Person { Name = "张三", Age = 22, Address = "广东省廉江市车板镇大坝村" });
            this.listbox0.Items.Add(new Person { Name = "李四", Age = 23, Address = "江西省景德镇市市辖区" });
            this.listbox0.Items.Add(new Person { Name = "王五", Age = 24, Address = "上海市虹口区" });
        }

        private void Button_Click(object sender, RoutedEventArgs e) {
            if (!(sender is Button button)) return;

            string name = button.Name;
            if (name == this.Button0.Name) {
                int selectedIndex = listbox0.SelectedIndex;
                object selectedItem = listbox0.SelectedItem;
                object selectedValue = listbox0.SelectedValue;
                _TextBlock0.Text = $"selectedIndex = {selectedIndex}, selectedItem = {selectedItem}, selectedValue = {selectedValue}";
                return;
            }
            if (name == this.Button1.Name) {
                int selectedIndex = listbox1.SelectedIndex;
                object selectedItem = listbox1.SelectedItem;
                object selectedValue = listbox1.SelectedValue;
                object content = null;
                if (selectedItem is ListBoxItem listBoxItem) {
                    content = listBoxItem.Content;
                }
                _TextBlock1.Text = $"selectedIndex = {selectedIndex}, selectedItem = {selectedItem}, selectedValue = {selectedValue}, content = {content}";
                return;
            }
        }
    }
}